package org.atovio.coolexam.service.impl;

import org.atovio.coolexam.entity.ExamPaperQuestionCustomerAnswer;
import org.atovio.coolexam.exception.ServiceException;
import org.atovio.coolexam.mapper.ExamPaperQuestionCustomerAnswerMapper;
import org.atovio.coolexam.service.ExamPaperQuestionCustomerAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ExamPaperQuestionCustomerAnswerServiceImpl implements ExamPaperQuestionCustomerAnswerService {

    private final ExamPaperQuestionCustomerAnswerMapper examPaperQuestionCustomerAnswerMapper;

    @Autowired
    public ExamPaperQuestionCustomerAnswerServiceImpl(ExamPaperQuestionCustomerAnswerMapper examPaperQuestionCustomerAnswerMapper) {
        this.examPaperQuestionCustomerAnswerMapper = examPaperQuestionCustomerAnswerMapper;
    }

    @Override
    public int getCount(Map<String, Object> criteria) {
        return examPaperQuestionCustomerAnswerMapper.selectCountSelective(criteria);
    }

    @Override
    public ExamPaperQuestionCustomerAnswer add(ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer) {
        int insert = examPaperQuestionCustomerAnswerMapper.insert(examPaperQuestionCustomerAnswer);
        if (insert != 1) {
            throw new ServiceException("数据库错误");
        }
        Map<String, Object> criteria = new HashMap<>();
        criteria.put("createUserId", examPaperQuestionCustomerAnswer.getCreateUserId());
        criteria.put("examPaperId", examPaperQuestionCustomerAnswer.getExamPaperId());
        criteria.put("questionId", examPaperQuestionCustomerAnswer.getQuestionId());
        criteria.put("examPaperAnswerId", examPaperQuestionCustomerAnswer.getExamPaperAnswerId());
        List<ExamPaperQuestionCustomerAnswer> answers =
                examPaperQuestionCustomerAnswerMapper.selectSelective(criteria);
        return answers.get(0);
    }

    @Override
    public ExamPaperQuestionCustomerAnswer modifyAnswerById(Integer id, String answer) {
        int i = examPaperQuestionCustomerAnswerMapper.updateAnswerById(id, answer);
        if (i != 1)
            throw new ServiceException("数据库错误");
        return examPaperQuestionCustomerAnswerMapper.selectById(id);
    }

    @Override
    public ExamPaperQuestionCustomerAnswer modifyCustomerScoreById(Integer id, Integer customerScore) {
        int count = examPaperQuestionCustomerAnswerMapper.updateCustomerScoreById(id, customerScore);
        if (count != 1)
            throw new ServiceException("数据库内部错误");
        return examPaperQuestionCustomerAnswerMapper.selectById(id);
    }

    @Override
    public List<ExamPaperQuestionCustomerAnswer> getSelective(Map<String, Object> criteria) {
        return examPaperQuestionCustomerAnswerMapper.selectSelective(criteria);
    }
}
